var bLoadOptionsDebugOutput = false;
// LOAD
function loadBooleanOption(bDefault, sLocalStorageName) {
  var sSavedValue = localStorage[sLocalStorageName];
  var bValue = (sSavedValue ? sSavedValue == 'true' : bDefault == true) ? true : false;
  if (bLoadOptionsDebugOutput) console.log('  Loading boolean option ' + repr(sLocalStorageName) + ' = ' + repr(bValue));
  return bValue;
}

function loadIntegerOption(iDefault, sLocalStorageName) {
  var sSavedValue = localStorage[sLocalStorageName];
  var iSavedValue = sSavedValue ? parseInt(sSavedValue) : NaN;
  var iValue = (!isNaN(iSavedValue) ? iSavedValue : (iDefault ? iDefault : 0));
  if (bLoadOptionsDebugOutput) console.log('  Loading integer option ' + repr(sLocalStorageName) + ' = ' + repr(iValue));
  return iValue;
}

function loadKeyDescriptionOption(sDefault, sLocalStorageName) {
  var sSavedValue = localStorage[sLocalStorageName];
  var sValue = (sSavedValue ? sSavedValue : (sDefault ? sDefault : ''));
  if (bLoadOptionsDebugOutput) console.log('  Loading key description option ' + repr(sLocalStorageName) + ' = ' + repr(sValue));
  return sValue;
}

function loadNumberOption(nDefault, sLocalStorageName) {
  var sSavedValue = localStorage[sLocalStorageName];
  var nSavedValue = sSavedValue ? parseFloat(sSavedValue) : NaN;
  var nValue = (!isNaN(nSavedValue) ? nSavedValue : (nDefault ? nDefault : 0));
  if (bLoadOptionsDebugOutput) console.log('  Loading number option ' + repr(sLocalStorageName) + ' = ' + repr(nValue));
  return nValue;
}

function loadOptions(oStructure) {
  var oOptions = loadOptionsGroup(oStructure, '');
  if (bLoadOptionsDebugOutput) console.log('Options = ' + repr(oOptions));
  return oOptions;
}

function loadOptionsGroup(oStructure, sGroupName) {
  if (bLoadOptionsDebugOutput) 
      if (sGroupName) {
        console.log('  Loading ' + repr(sGroupName) + ' options...');
      } else {
        console.log('Loading options...');
      }
  var oValues = {};
  for (var sMemberName in oStructure) {
    var oMemberStructure = oStructure[sMemberName];
    var sLocalStorageName = (sGroupName ? sGroupName + '.' : '') + sMemberName;
    switch (oMemberStructure.type) {
      case 'boolean':
        oValues[sMemberName] = loadBooleanOption(oMemberStructure.defaultValue, sLocalStorageName);
        break;
      case 'group':
        oValues[sMemberName] = loadOptionsGroup(oMemberStructure.members, sLocalStorageName);
        break;
      case 'integer':
        oValues[sMemberName] = loadIntegerOption(oMemberStructure.defaultValue, sLocalStorageName);
        break;
      case 'key description':
        oValues[sMemberName] = loadKeyDescriptionOption(oMemberStructure.defaultValue, sLocalStorageName);
        break;
      case 'list':
        oValues[sMemberName] = loadOptionsList(oMemberStructure.items, sLocalStorageName);
        break;
      case 'number':
        oValues[sMemberName] = loadNumberOption(oMemberStructure.defaultValue, sLocalStorageName);
        break;
      case 'text':
        oValues[sMemberName] = loadTextOption(oMemberStructure.defaultValue, sLocalStorageName);
        break;
      default:
        assertionError('Unknown option type ' + repr(oMemberStructure.type) + '.');
    }
  }
  return oValues;
}

function loadOptionsList(oStructure, sLocalStorageName) {
  var sCount = localStorage[sLocalStorageName + '.length'];
  var iCount = sCount ? parseInt(sCount) : 0;
  if (bLoadOptionsDebugOutput) console.log('  Loading menu list ' + repr(sLocalStorageName) + ' (' + iCount + ' entries)');
  var aValues = [];
  for (var i = 0; i < iCount; i++) {
    aValues.push(loadOptionsGroup(oStructure, sLocalStorageName + '[' + i + ']'));
  }
  return aValues;
}

function loadTextOption(sDefault, sLocalStorageName) {
  var sSavedValue = localStorage[sLocalStorageName];
  var sValue = (sSavedValue ? sSavedValue : (sDefault ? sDefault : ''));
  if (bLoadOptionsDebugOutput) console.log('  Loading text option ' + repr(sLocalStorageName) + ' = ' + repr(sValue));
  return sValue;
}
